package com.cjlgb.design.upms.controller;

import com.cjlgb.design.common.core.constant.InnerApps;
import com.cjlgb.design.common.security.annotation.Inner;
import com.cjlgb.design.upms.api.entity.SysMenu;
import com.cjlgb.design.upms.service.SysMenuService;
import com.cjlgb.design.upms.service.SysRoleMenuService;
import com.cjlgb.design.upms.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author WFT
 * @date 2019/7/26
 * description: 账号控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/account")
public class AccountController {

    private final SysUserRoleService userRoleService;
    private final SysRoleMenuService roleMenuService;
    private final SysMenuService menuService;

    /**
     * 获取账号的权限列表
     * @param accountId 账号Id
     * @return Collection<String>
     */
    @Inner(value = InnerApps.OAUTH_SERVER)
    @GetMapping(value = "/getAccountAuthorities")
    public Collection<String> getAccountAuthorities(@RequestParam(value = "accountId") Long accountId){
        //  获取角色Id列表
        Collection<Long> roleIds = this.userRoleService.selectRoleIds(accountId);
        if (roleIds.size() > 0){
            //  获取权限Id列表
            Collection<Long> authorities = this.roleMenuService.selectRoleAuthorities(roleIds);
            if (!authorities.isEmpty()){
                return this.menuService.listByIds(authorities)
                        .stream().map(SysMenu::getPermission).collect(Collectors.toList());
            }
        }
        return new ArrayList<>(0);
    }

}
